package com.meida.module.bus.provider.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.log.StaticLog;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.common.security.OpenHelper;
import com.meida.common.security.OpenUser;
import com.meida.common.utils.DateUtils;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.*;
import com.meida.module.bus.provider.api.service.SyncOAService;
import com.meida.module.bus.provider.api.service.SyncSrmService;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.api.util.PmsOaUtil;
import com.meida.module.bus.provider.mapper.BusMaterialPackage200Mapper;
import com.meida.module.bus.provider.service.BusMaterialPackage200Service;
import com.meida.module.bus.provider.service.BusMaterialService;
import com.meida.module.bus.provider.service.BusProjectService;
import com.meida.module.bus.provider.service.BusVersionService;
import com.meida.module.bus.provider.utils.CommonUtil;
import com.meida.module.bus.provider.utils.LevelNoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 200分包接口实现类
 *
 * @author flyme
 * @date 2020-10-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusMaterialPackage200ServiceImpl extends BaseServiceImpl<BusMaterialPackage200Mapper, BusMaterialPackage200> implements BusMaterialPackage200Service {

    @Autowired
    BusVersionService busVersionService;

    @Autowired
    BusMaterialService busMaterialService;

    @Autowired
    BusProjectService busProjectService;

    @Autowired
    PmsOaUtil pmaOaUtil;

    @Autowired
    SyncSrmService syncSrmService;

    @Autowired
    LevelNoUtil levelNoUtil;

    @Autowired
    SyncOAService syncOAService;

    @Autowired
    CommonUtil commonUtil;

    @Autowired
    private PmsApiUtil pmsApiUtil;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusMaterialPackage200 bmp, EntityMap extra) {
        Long projectId = TypeUtils.castToLong(bmp.getProjectId());
        BusProject busProject = busProjectService.getById(projectId);
        if (FlymeUtils.isEmpty(busProject)) {
            return ResultBody.failed("项目信息不存在");
        }
        Integer state = FlymeUtils.getInteger(busProject.getState(), 0);
        if (state == 1) {
            return ResultBody.failed("项目已锁定");
        }
        return ResultBody.ok();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusMaterialPackage200> cq, BusMaterialPackage200 bmp, EntityMap requestMap) {
        cq.addSelect(BusMaterialPackage200.class, "*");
        cq.addSelect(BusProject.class, "projectName", "projectCode");
        cq.addSelect(BusMaterial.class, "unitBillQuantity100", "convertBillQuantity100", "planPurchaseCount", "convertPurchaseCount", "measurementUnit");
        //项目代码
        cq.like(BusProject.class, "projectCode", requestMap.get("projectCode"));
        //项目名称
        cq.like(BusProject.class, "projectName", requestMap.get("projectName"));
        //专业包代码
        cq.like(BusMaterialPackage200.class, "majorCode", requestMap.get("majorCode"));
        //专业包名称
        cq.like(BusMaterialPackage200.class, "majorName", requestMap.get("majorName"));
        //工程包代码
        cq.like(BusMaterialPackage200.class, "packageNo", requestMap.get("packageNo"));
        //工程包名称
        cq.like(BusMaterialPackage200.class, "packageName", requestMap.get("packageName"));
        //服务WBS
        cq.like(BusMaterialPackage200.class, "serviceWbsCode", requestMap.get("serviceWbsCode"));
        //服务代码
        cq.like(BusMaterialPackage200.class, "serviceCode", requestMap.get("serviceCode"));
        //科目名称
        cq.like(BusMaterialPackage200.class, "subjectName", requestMap.get("subjectName"));
        //材料名称
        cq.like(BusMaterialPlan100.class, "materialName", requestMap.get("materialName"));
        //科目名称
        cq.like(BusMaterialPlan100.class, "materiaSpecs", requestMap.get("materiaSpecs"));
        //区域
        cq.like(BusMaterialPackage100.class, "materialArea", requestMap.get("materialArea"));
        //系统
        cq.like(BusMaterialPackage100.class, "materialUse", requestMap.get("materialUse"));
        //层级号
        cq.like(BusMaterialPackage100.class, "levelNo", requestMap.get("levelNo"));
        cq.eq("bmp.projectId", requestMap.get("projectId"));
        cq.eq("versionStatus", requestMap.get("versionStatus"));
        cq.eq("planType", requestMap.get("planType"));
        Boolean isGroup = requestMap.getBoolean("isGroup");
        //100分包列表数据
        if (isGroup) {
            cq.groupBy("projectId", "packageNo", "majorCode");
        } else {
            //100分包详情列表
            cq.eq("bmp.packageNo", requestMap.get("packageNo"));
            cq.eq("bmp.majorCode", requestMap.get("majorCode"));
            Boolean isReport = requestMap.getBoolean("isReport");
            if (isReport) {
                cq.eq("bmp.parentId", 0);
            }
        }
        cq.eq("bmp.versionId", requestMap.get("versionId"));
        cq.createJoin(BusMaterial.class).setJoinField("materialId").setMainField("materialId");
        cq.createJoin(BusProject.class).setJoinField("projectId").setMainField("projectId");
        if (!isGroup) {
            Boolean isReport = requestMap.getBoolean("isReport");
            if (isReport) {
                if(!pmsApiUtil.ckPackageIdForSrmContract(Long.valueOf(requestMap.get("projectId")), requestMap.get("packageNo"))) {
                    cq.createJoin(BusMaterialSrmSub.class).inner().setJoinField("materialPackageId").setMainField("packagePlan100Id");
                } else {
                    cq.createJoin(BusMaterialSrmSub.class).inner().setJoinField("materialPackageId").setMainField("materialPackage100Id");
                }
                cq.groupBy("materialPackage100Id");
            }
        }
        cq.orderByAsc("bmp.levelNo");
//        cq.orderByDesc("bmp.createTime");
        return ResultBody.ok();
    }

    @Override
    public List<EntityMap> selectPackage200Details(Map map) {
        CriteriaQuery<BusMaterialPackage200> cq = new CriteriaQuery(BusMaterialPackage200.class);
        cq.addSelect(BusMaterialPackage200.class, "*");
        cq.addSelect(BusProject.class, "projectCode", "projectName");
        cq.addSelect(BusMaterial.class, "contractBillQuantity", "beforeMarkQuantity", "installPrice", "syntheticalPrice", "installPrice1", "syntheticalPrice1", "installPrice2", "syntheticalPrice2", "measurementUnit");
        cq.eq("bmp.projectId", map.get("projectId"));
        cq.eq("bmp.packageNo", map.get("packageNo"));
        cq.eq("bmp.majorCode", map.get("majorCode"));
        cq.like("serviceWbsCode", map.get("serviceWbsCode"));
        cq.like("serviceCode", map.get("serviceCode"));
        cq.like("levelNo", map.get("levelNo"));
        cq.like("subjectName", map.get("subjectName"));
        cq.like("materialArea", map.get("materialArea"));//系统
        cq.like("materialUse", map.get("materialUse"));
//        cq.orderByAsc("bmp.createTime");
        cq.orderByAsc("bmp.levelNo");
        cq.createJoin(BusProject.class).setMainField("projectId").setJoinField("projectId");
        cq.createJoin(BusMaterial.class).setMainField("materialPackage100Id").setJoinField("materialId");
        return selectEntityMap(cq);
    }

    @Override
    public List<EntityMap> selectPackage200RootDetails(Map map) {
        CriteriaQuery<BusMaterialPackage200> cq = new CriteriaQuery(BusMaterialPackage200.class);
        cq.addSelect(BusMaterialPackage200.class, "*");
        cq.addSelect(BusProject.class, "projectCode", "projectName");
        cq.addSelect(BusMaterial.class, "contractBillQuantity", "beforeMarkQuantity", "installPrice", "syntheticalPrice", "installPrice1", "syntheticalPrice1", "installPrice2", "syntheticalPrice2");
        cq.eq("bmp.projectId", map.get("projectId"));
        cq.eq("bmp.packageNo", map.get("packageNo"));
        cq.eq("bmp.majorCode", map.get("majorCode"));
        cq.eq("bmp.parentId", 0);
        cq.orderByAsc("bmp.createTime");
        cq.createJoin(BusProject.class).setMainField("projectId").setJoinField("projectId");
        cq.createJoin(BusMaterial.class).setMainField("materialPackage100Id").setJoinField("materialId");
        return selectEntityMap(cq);
    }

    @Override
    public ResultBody copyAndUpdate(Map map) {
        String copyDataStr = (String) map.get("copyDataStr");
        if (FlymeUtils.isEmpty(copyDataStr)) {
            return ResultBody.failed("没有要提交的数据");
        }
        Long projectId = TypeUtils.castToLong(map.get("projectId"));
        BusProject busProject = busProjectService.getById(projectId);
        if (FlymeUtils.isEmpty(busProject)) {
            return ResultBody.failed("项目信息不存在");
        }
        Integer state = FlymeUtils.getInteger(busProject.getState(), 0);
        if (state == 1) {
            return ResultBody.failed("项目已锁定");
        }
        Integer type = TypeUtils.castToInt(map.get("type"));
        BusVersion newBusVersion = busVersionService.getNewBusVersion(projectId, 2, type);
        Long versionId = newBusVersion.getVersionId();
        busVersionService.save(newBusVersion);
        List<BusMaterialPackage200> busMaterialPackage200List = JSON.parseArray(copyDataStr, BusMaterialPackage200.class);
        List<BusMaterial> busMaterialList = new ArrayList<>();
        for (BusMaterialPackage200 busMaterialPackage200 : busMaterialPackage200List) {
            long id = IdWorker.getId();
            Long materialId = busMaterialPackage200.getMaterialId();
            Long parentId = busMaterialPackage200.getParentId();
            busMaterialPackage200.setMaterialPackage100Id(id);
            if (parentId.longValue() == 0L) {
                busMaterialPackage200.setParentId(materialId);
            } else {
                busMaterialPackage200.setParentId(parentId);
            }
            Long parentId1 = busMaterialPackage200.getParentId();
            String packageNo = busMaterialPackage200.getPackageNo();
            String levelNo = busMaterialPackage200.getLevelNo();
            String redisKey = "package200_" + parentId1;
            levelNo = levelNoUtil.getLevelNo(redisKey, packageNo, levelNo);
            busMaterialPackage200.setLevelNo(levelNo);
            busMaterialPackage200.setPlanType(type);
            busMaterialPackage200.setVersionId(versionId);
            busMaterialPackage200.setParentId(materialId);
            busMaterialPackage200.setCreateTime(DateUtils.getNowDateTime());
            busMaterialPackage200.setUpdateTime(null);
            busMaterialPackage200.setVersionStatus(0);
            busMaterialPackage200.setMaterialId(id);
            busMaterialPackage200.setApplayStatus(0);
            BusMaterial busMaterial = busMaterialService.getById(materialId);
            BusMaterial busMaterial1 = new BusMaterial();
            BeanUtils.copyProperties(busMaterial, busMaterial1);
            busMaterial1.setMaterialId(id);
            busMaterial1.setParentId(parentId1);
            busMaterial1.setCreateTime(DateUtils.getNowDateTime());
            busMaterial1.setUpdateTime(null);
            busMaterialList.add(busMaterial1);
        }
        if (busMaterialPackage200List.size() > 0) {
            saveBatch(busMaterialPackage200List);
            busMaterialService.saveBatch(busMaterialList);
            busProject.setVersionpackageId(versionId);
            busProjectService.updateById(busProject);
        } else {
            return ResultBody.failed("修正失败");
        }
        return ResultBody.ok();
    }

    @Override
    public Boolean updateApplayStatus(Long projectId, String packageNo, String majorCode, Integer planType, Integer applayStatus) {
        CriteriaUpdate<BusMaterialPackage200> cu = new CriteriaUpdate<>();
        cu.eq("projectId", projectId);
        cu.eq("majorCode", majorCode);
        cu.eq("packageNo", packageNo);
        cu.eq("planType", planType);
        cu.eq("versionStatus", 0);
        cu.set("applayStatus", applayStatus);
        return update(cu);
    }


    @Override
    public ResultBody subMerge200OaCheck(Map map) {
        Long projectId = FlymeUtils.getLong(map.get("projectId"));
        BusProject busProject = busProjectService.getById(projectId);
        if (FlymeUtils.isEmpty(busProject)) {
            return ResultBody.failed("项目信息不存在");
        }
        Integer state = FlymeUtils.getInteger(busProject.getState(), 0);
        if (state == 1) {
            return ResultBody.failed("项目已锁定");
        }
        String majorCode = (String) map.get("majorCode");
        String packageNo = (String) map.get("packageNo");
        Integer type = TypeUtils.castToInt(map.get("type"));
        String userName = OpenHelper.getUserName();
        // 设定请求MAP
        pmsApiUtil.setPlanPackageDataToReqMap(map, projectId, packageNo, majorCode, type, type == 1 ? ApiConstants.ENGINEER_APPROVAL_FB_200_UPD : ApiConstants.ENGINEER_APPROVAL_FB_200_ADD);
        updateApplayStatus(projectId, packageNo, majorCode, type, 1);
        int approvalResult = 0;
        if (type == 1) {
            approvalResult = syncOAService.approval(map, "/approvl/bmp200UpdateDetail", userName, ApiConstants.ENGINEER_APPROVAL_FB_200_UPD, projectId.toString());

        }
        if (type == 2) {
            approvalResult = syncOAService.approval(map, "/approvl/bmp200AddDetail", userName, ApiConstants.ENGINEER_APPROVAL_FB_200_ADD, projectId.toString());

        }
        if (ApiConstants.APPROVAL_SUCCESS == approvalResult) {
            return ResultBody.ok("OA审批提交成功");
        } else {
            updateApplayStatus(projectId, packageNo, majorCode, type, 0);
        }
        return ResultBody.ok("OA审批提交失败");

    }

    @Override
    public ResultBody goMerge200(Long projectId, String packageNo, String majorCode, String userName) throws Exception {
//        String userName = OpenHelper.getUserName();
        Boolean b = pmaOaUtil.updateMaterialBaseForPackage(projectId, majorCode, packageNo, ApiConstants.VER_200, userName);
        if (b) {
            List<BusMaterialPackage200> busMaterialPackage200List = selectBusMaterialPackage200List(projectId, majorCode, packageNo);
            for (BusMaterialPackage200 busMaterialPackage200 : busMaterialPackage200List) {
                busMaterialPackage200.setVersionStatus(1);
            }
            if (busMaterialPackage200List.size() > 0) {
                updateBatchById(busMaterialPackage200List);
            }
            return ResultBody.ok("合并子节点成功");
        }
        return ResultBody.failed("合并子节点失败");
    }

    @Override
    public EntityMap sumSupplierNum(Long projectId, String majorCode, String packageNo, String SupplierNumField) {
        CriteriaQuery<BusMaterialPackage200> cq = new CriteriaQuery(BusMaterialPackage200.class);
        cq.addSelect("COALESCE(SUM(" + SupplierNumField + "),0) totalSupplierNum");
        cq.eq("projectId", projectId);
        cq.eq("majorCode", majorCode);
        cq.eq("packageNo", packageNo);
        return findOne(cq);
    }

    @Override
    public ResultBody importOnUpdateBusMaterialPackage200Data(MultipartFile file, Map params) {
        try {
            Long projectId = FlymeUtils.getLong(params.get("projectId"));
            BusProject busProject = busProjectService.getById(projectId);
            if (FlymeUtils.isEmpty(busProject)) {
                return ResultBody.failed("项目信息不存在");
            }
            Integer state = FlymeUtils.getInteger(busProject.getState(), 0);
            if (state == 1) {
                return ResultBody.failed("项目已锁定");
            }
            List<BusMaterialPackage200> busMaterialPackage200List = new ArrayList<>();
            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(1);
            List<Map<Object, Object>> ExcelList = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, importParams);
            System.out.println(ExcelList.size());
            for (Map<Object, Object> objectMap : ExcelList) {
                Long materialPackage100Id = TypeUtils.castToLong(objectMap.get("分包Id"));
                BusMaterialPackage200 materialPackage200 = getById(materialPackage100Id);
                if (FlymeUtils.isNotEmpty(materialPackage200)) {
                    BigDecimal supplierNum1 = TypeUtils.castToBigDecimal(objectMap.get("包商1合同量"));
                    BigDecimal supplierNum2 = TypeUtils.castToBigDecimal(objectMap.get("包商2合同量"));
                    BigDecimal supplierNum3 = TypeUtils.castToBigDecimal(objectMap.get("包商3合同量"));
                    BigDecimal supplierNum4 = TypeUtils.castToBigDecimal(objectMap.get("包商4合同量"));
                    BigDecimal supplierNum5 = TypeUtils.castToBigDecimal(objectMap.get("包商5合同量"));
                    BigDecimal supplierNum6 = TypeUtils.castToBigDecimal(objectMap.get("包商6合同量"));
                    BigDecimal supplierNum7 = TypeUtils.castToBigDecimal(objectMap.get("包商7合同量"));
                    BigDecimal supplierNum8 = TypeUtils.castToBigDecimal(objectMap.get("包商8合同量"));
                    BigDecimal supplierNum9 = TypeUtils.castToBigDecimal(objectMap.get("包商9合同量"));
                    BigDecimal supplierNum10 = TypeUtils.castToBigDecimal(objectMap.get("包商10合同量"));
                    materialPackage200.setSupplierNum1(supplierNum1);
                    materialPackage200.setSupplierNum2(supplierNum2);
                    materialPackage200.setSupplierNum3(supplierNum3);
                    materialPackage200.setSupplierNum4(supplierNum4);
                    materialPackage200.setSupplierNum5(supplierNum5);
                    materialPackage200.setSupplierNum6(supplierNum6);
                    materialPackage200.setSupplierNum7(supplierNum7);
                    materialPackage200.setSupplierNum8(supplierNum8);
                    materialPackage200.setSupplierNum9(supplierNum9);
                    materialPackage200.setSupplierNum10(supplierNum10);
                    busMaterialPackage200List.add(materialPackage200);
                }
            }
            if (busMaterialPackage200List.size() > 0) {
                updateBatchById(busMaterialPackage200List);
                return ResultBody.ok("导入成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBody.failed("导入失败");
    }

    @Override
    public ResultBody importOnAddBusMaterialPackage200Data(MultipartFile file, Map params) {
        try {
            Long projectId = TypeUtils.castToLong(params.get("projectId"));
            BusProject busProject = busProjectService.getById(projectId);
            if (FlymeUtils.isEmpty(busProject)) {
                return ResultBody.failed("项目信息不存在");
            }
            Integer state = FlymeUtils.getInteger(busProject.getState(), 0);
            if (state == 1) {
                return ResultBody.failed("项目已锁定");
            }
            Integer type = TypeUtils.castToInt(params.get("type"));
            String majorName = (String) params.get("majorName");
            String packageName = (String) params.get("packageName");
            List<BusMaterialPackage200> busMaterialPackage200List = new ArrayList<>();
            List<BusMaterial> busMaterialList = new ArrayList<>();
            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(1);
            List<Map<Object, Object>> ExcelList = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, importParams);
            if (commonUtil.ckLevelNo(projectId, ApiConstants.FB_PLANDATA_100, ExcelList)) {
                return ResultBody.failed("存在重复的层级号");
            }
            String majorCode = (String) params.get("majorCode");
            String packageNo = (String) params.get("packageNo");
            BusVersion busVersion = busVersionService.getNewBusVersion(projectId, 2, type);
            busVersionService.save(busVersion);
            ResultBody resultBody = pmsApiUtil.chkImpExcelContentForPlanData(ExcelList, ApiConstants.FB_PLANDATA);
            if(!resultBody.isOk()) {
                return resultBody;
            }
            for (Map<Object, Object> objectMap : ExcelList) {
                String isAddStr = TypeUtils.castToString(objectMap.get("是否新增"));
                isAddStr = FlymeUtils.getString(isAddStr, "是");
                if ("是".equals(isAddStr)) {
                    if (FlymeUtils.isNotEmpty(busProject)) {
                        Long materialPackage100Id = TypeUtils.castToLong(objectMap.get("分包Id"));
                        String serviceCode = TypeUtils.castToString(objectMap.get("服务代码"));
                        String serviceWbsCode = TypeUtils.castToString(objectMap.get("服务WBS代码"));
                        String subjectName = TypeUtils.castToString(objectMap.get("科目名称"));
                        String levelNo = TypeUtils.castToString(objectMap.get("层级号"));
                        String packageDesc = TypeUtils.castToString(objectMap.get("分承包发包项目描述"));
                        String contractDesc = TypeUtils.castToString(objectMap.get("对外合同描述"));
                        String materialArea = TypeUtils.castToString(objectMap.get("区域"));
                        String materialUse = TypeUtils.castToString(objectMap.get("系统"));
                        BigDecimal beforeMarkQuantity = TypeUtils.castToBigDecimal(objectMap.get("标前工程量"));
                        BigDecimal costQuantity200 = TypeUtils.castToBigDecimal(objectMap.get("200预算成本工程量"));
                        String measurementUnit = TypeUtils.castToString(objectMap.get("计量单位"));
                        BigDecimal unitBillQuantity100 = TypeUtils.castToBigDecimal(objectMap.get("100计量单位工程量合计"));
                        BigDecimal convertBillQuantity100 = TypeUtils.castToBigDecimal(objectMap.get("100换算单位工程量合计"));
                        BigDecimal supplierNum1 = TypeUtils.castToBigDecimal(objectMap.get("包商1合同量"));
                        BigDecimal supplierNum2 = TypeUtils.castToBigDecimal(objectMap.get("包商2合同量"));
                        BigDecimal supplierNum3 = TypeUtils.castToBigDecimal(objectMap.get("包商3合同量"));
                        BigDecimal supplierNum4 = TypeUtils.castToBigDecimal(objectMap.get("包商4合同量"));
                        BigDecimal supplierNum5 = TypeUtils.castToBigDecimal(objectMap.get("包商5合同量"));
                        BigDecimal supplierNum6 = TypeUtils.castToBigDecimal(objectMap.get("包商6合同量"));
                        BigDecimal supplierNum7 = TypeUtils.castToBigDecimal(objectMap.get("包商7合同量"));
                        BigDecimal supplierNum8 = TypeUtils.castToBigDecimal(objectMap.get("包商8合同量"));
                        BigDecimal supplierNum9 = TypeUtils.castToBigDecimal(objectMap.get("包商9合同量"));
                        BigDecimal supplierNum10 = TypeUtils.castToBigDecimal(objectMap.get("包商10合同量"));
                        BigDecimal installPrice = TypeUtils.castToBigDecimal(objectMap.get("预算成本单价（未税）_单价_安装价"));
                        BigDecimal syntheticalPrice = TypeUtils.castToBigDecimal(objectMap.get("预算成本单价（未税）_单价_综合单价"));
                        BigDecimal installPrice1 = TypeUtils.castToBigDecimal(objectMap.get("对外合同单价_有合同单价_安装价"));
                        BigDecimal syntheticalPrice1 = TypeUtils.castToBigDecimal(objectMap.get("对外合同单价_有合同单价_综合单价"));
                        BigDecimal installPrice2 = TypeUtils.castToBigDecimal(objectMap.get("对外合同单价_拟合同单价_安装价"));
                        BigDecimal syntheticalPrice2 = TypeUtils.castToBigDecimal(objectMap.get("对外合同单价_拟合同单价_综合单价"));
                        BusMaterial busMaterial = new BusMaterial();
                        long materialId = IdWorker.getId();
                        busMaterial.setMaterialId(materialId);
                        busMaterial.setProjectId(projectId);
                        busMaterial.setMeasurementUnit(measurementUnit);
                        busMaterial.setUnitBillQuantity100(unitBillQuantity100);
                        busMaterial.setConvertBillQuantity100(convertBillQuantity100);
                        busMaterial.setInstallPrice(installPrice);
                        busMaterial.setInstallPrice1(installPrice1);
                        busMaterial.setInstallPrice2(installPrice2);
                        busMaterial.setSyntheticalPrice(syntheticalPrice);
                        busMaterial.setSyntheticalPrice1(syntheticalPrice1);
                        busMaterial.setSyntheticalPrice2(syntheticalPrice2);
                        busMaterial.setBeforeMarkQuantity(beforeMarkQuantity);
                        BusMaterialPackage200 busMaterialPackage200 = new BusMaterialPackage200();
                        busMaterialPackage200.setMaterialPackage100Id(materialId);
                        if (FlymeUtils.isEmpty(materialPackage100Id)) {
                            busMaterialPackage200.setParentId(0L);
                        } else {
                            BusMaterialPackage200 materialPackage200 = getById(materialPackage100Id);
                            Long parentId = materialPackage200.getParentId();
                            if (parentId.longValue() == 0L) {
                                busMaterialPackage200.setParentId(materialPackage100Id);
                                busMaterialPackage200.setTopParentId(materialId);
                            } else {
                                busMaterialPackage200.setParentId(parentId);
                                busMaterialPackage200.setTopParentId(parentId);
                            }
                        }
                        busMaterial.setParentId(busMaterialPackage200.getParentId());
                        busMaterialList.add(busMaterial);
                        busMaterialPackage200.setTopParentId(materialId);
                        busMaterialPackage200.setProjectId(projectId);
                        busMaterialPackage200.setMaterialId(materialId);
                        busMaterialPackage200.setPackageNo(packageNo);
                        busMaterialPackage200.setPackageName(packageName);
                        busMaterialPackage200.setMajorName(majorName);
                        busMaterialPackage200.setMajorCode(majorCode);
                        busMaterialPackage200.setServiceCode(serviceCode);
                        busMaterialPackage200.setServiceWbsCode(serviceWbsCode);
                        busMaterialPackage200.setSubjectName(subjectName);
                        busMaterialPackage200.setLevelNo(levelNo);
                        busMaterialPackage200.setCostQuantity100(BigDecimal.ZERO);
                        busMaterialPackage200.setCostQuantity200(costQuantity200);
                        busMaterialPackage200.setMaterialArea(materialArea);
                        busMaterialPackage200.setMaterialUse(materialUse);
                        busMaterialPackage200.setPackageDesc(packageDesc);
                        busMaterialPackage200.setContractDesc(contractDesc);
                        busMaterialPackage200.setSupplierNum1(supplierNum1);
                        busMaterialPackage200.setSupplierNum2(supplierNum2);
                        busMaterialPackage200.setSupplierNum3(supplierNum3);
                        busMaterialPackage200.setSupplierNum4(supplierNum4);
                        busMaterialPackage200.setSupplierNum5(supplierNum5);
                        busMaterialPackage200.setSupplierNum6(supplierNum6);
                        busMaterialPackage200.setSupplierNum7(supplierNum7);
                        busMaterialPackage200.setSupplierNum8(supplierNum8);
                        busMaterialPackage200.setSupplierNum9(supplierNum9);
                        busMaterialPackage200.setSupplierNum10(supplierNum10);
                        busMaterialPackage200.setPlanType(type);
                        busMaterialPackage200.setVersionId(busVersion.getVersionId());
                        busMaterialPackage200.setApplayStatus(0);
                        busMaterialPackage200.setVersionStatus(0);
                        busMaterialPackage200List.add(busMaterialPackage200);
                    }
                }
            }
            if (busMaterialList.size() > 0) {
                saveBatch(busMaterialPackage200List);
                busMaterialService.saveBatch(busMaterialList);
                busProject.setVersionId(busVersion.getVersionId());
                busProjectService.updateById(busProject);
                return ResultBody.ok("导入成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBody.failed("导入失败");
    }

    @Override
    public ResultBody getSrmPms05Req(Map map) throws Exception {
        Long projectId = FlymeUtils.getLong(map.get("projectId"));
        BusProject busProject = busProjectService.getById(projectId);
        if (FlymeUtils.isEmpty(busProject)) {
            return ResultBody.failed("项目信息不存在");
        }
        Integer state = FlymeUtils.getInteger(busProject.getState(), 0);
        if (state == 1) {
            return ResultBody.failed("项目已锁定");
        }
        String majorCode = TypeUtils.castToString(map.get("majorCode"));
        String packageNo = TypeUtils.castToString(map.get("packageNo"));
        OpenUser user = OpenHelper.getUser();
        String username = user.getUsername();
        Boolean b = pmaOaUtil.updateMaterialBaseForPackageSingleNoSync(projectId, majorCode, packageNo, ApiConstants.VER_200, username);
        if (b) {
            int i = syncSrmService.planCreate(projectId, majorCode, packageNo, ApiConstants.VER_200, username);
            if (i == 0) {
                return ResultBody.ok("调用成功");
            }
        }
        return ResultBody.failed("调用失败");
    }

    @Override
    public List<EntityMap> selectVersionList(Map map) {
        String planType = (String) map.get("planType");
        CriteriaQuery<BusMaterialPackage200> cq = new CriteriaQuery(BusMaterialPackage200.class);
        cq.select(BusVersion.class, "versionId", "concat(bigVersion,'.',smallVerion) as versionNo");
        cq.eq("bmp.projectId", map.get("projectId"));
        cq.eq("bmp.packageNo", map.get("packageNo"));
        cq.eq("bmp.majorCode", map.get("majorCode"));
        cq.isNotNull("version.versionId");
        cq.eq("bmp.planType", planType);
        cq.createJoin(BusVersion.class).setMainField("versionId").setJoinField("versionId");
        cq.groupBy("bmp.versionId");
        return selectEntityMap(cq);
    }

    @Override
    public List<BusMaterialPackage200> selectBusMaterialPackage200List(Long projectId, String majorCode, String packageNo) {
        CriteriaQuery<BusMaterialPackage200> cq = new CriteriaQuery(BusMaterialPackage200.class);
        cq.eq("projectId", projectId);
        cq.eq("majorCode", majorCode);
        cq.eq("packageNo", packageNo);
        cq.eq("versionStatus", 0);
        return list(cq);
    }
}
